package com.wss.controller;


import com.alibaba.fastjson.JSONObject;
import com.wss.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@Controller
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/avatorImages/**").addResourceLocations("file:/Users/wangshuaishuai/Desktop/java/music-website-master/music-server/avatorImages/");
        }
    }

    //判断是否登录成功
    @ResponseBody
    @PostMapping("/user/login/status")
    public Object loginStatus(HttpServletRequest req, HttpSession session){

        JSONObject jsonObject = new JSONObject();
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        boolean res = consumerService.veritypasswd(username,password);
        if(res){
            jsonObject.put("code",1);
            jsonObject.put("msg","登录成功");
            jsonObject.put("userMsg",consumerService.loginStatus(username));
            session.setAttribute("username",username);
            return jsonObject;
        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","用户名或密码错误");
            return jsonObject;
        }
    }

    @RequestMapping(value = "/user/detail", method = RequestMethod.GET)
    public Object userOfId(HttpServletRequest req){
        String id = req.getParameter("id");
        return consumerService.userOfId(Integer.parseInt(id));
    }

}
